package com.ctcemti.mdm.project.sys.service.impl;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.ctcemti.mdm.common.util.FileUploadUtils;
import com.ctcemti.mdm.project.sys.dao.OperationLogMapper;
import com.ctcemti.mdm.project.sys.entity.OperationLog;
import com.ctcemti.mdm.project.sys.service.IOperationLogService;
import com.ctcemti.mdm.project.tool.generatecode.util.Convert;
import com.ctcemti.mdm.project.tool.generatecode.util.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * 操作日志Service业务层处理
 * 
 * @author 
 * @date Tue Nov 12 10:28:50 CST 2019
 */
@Service
public class OperationLogServiceImpl implements IOperationLogService 
{
    @Autowired
    private OperationLogMapper operationLogMapper;

    @Value("${ctcemti.errorfilepath}")
    private String errorfilepath;
    /**
     * 查询操作日志
     * 
     * @param id 操作日志ID
     * @return 操作日志
     */
    @Override
    public OperationLog selectOperationLogById(Long id)
    {
        return operationLogMapper.selectOperationLogById(id);
    }

    /**
     * 查询操作日志列表
     * 
     * @param operationLog 操作日志
     * @return 操作日志
     */
    @Override
    public PageInfo<OperationLog> selectOperationLogList(OperationLog operationLog)
    {
        PageHelper.startPage(operationLog.getPageIndex(),operationLog.getPageSize(),"tsol.id");
        List<OperationLog> list=operationLogMapper.selectOperationLogList(operationLog);
        PageInfo<OperationLog> pageInfo =  new PageInfo<OperationLog>(list);
        return pageInfo;
    }

    /**
     * 新增操作日志
     * 
     * @param operationLog 操作日志
     * @return 结果
     */
    @Override
    public int insertOperationLog(OperationLog operationLog)
    {
        return operationLogMapper.insertOperationLog(operationLog);
    }

    /**
     * 修改操作日志
     * 
     * @param operationLog 操作日志
     * @return 结果
     */
    @Override
    public int updateOperationLog(OperationLog operationLog)
    {
        return operationLogMapper.updateOperationLog(operationLog);
    }

    /**
     * 删除操作日志对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOperationLogByIds(String ids)
    {
        return operationLogMapper.deleteOperationLogByIds(Convert.toStrArray(ids));
    }

    @Override
    public void uploadFile(HttpServletResponse response, HttpServletRequest request, String filename) throws SQLException, IOException {
        OperationLog operationLog = new OperationLog();
        operationLog.setPageIndex(1);
        operationLog.setPageSize(Integer.MAX_VALUE);
        PageHelper.startPage(operationLog.getPageIndex(),operationLog.getPageSize(),"tsol.id");
        List<OperationLog> list=operationLogMapper.selectOperationLogList(operationLog);
        ExportParams exportParams = new ExportParams(null,null,filename);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,OperationLog.class, list);
        FileUploadUtils.downWorkbook(request,response,workbook,filename);

    }

}
